home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 8 code / TValidText / UValidText.p < prev    next >
Encoding:
Text File  |  1991-10-09  |  5.6 KB  |  192 lines  |  [TEXT/MPS ]

  1. {**********************************************************************************
  2. UValidText.p
  3.     Classes implementing the ability to enter strings which must be validated,
  4.     such as dates, times, numbers, and so on.
  5. **********************************************************************************}
  6.  
  7. UNIT UValidText;
  8.  
  9. INTERFACE
  10. USES
  11.     { • MacApp }
  12.     UMacApp,
  13.     UMacAppUtilities,
  14.     
  15.     { • building blocks }
  16.     UTEView,
  17.     UDialog,
  18.     
  19.     { • Implementation use }
  20.     ToolUtils;                            { for GetIndString() }
  21.  
  22. CONST
  23.     kValidTextErrorStrings    =  700;        { string list ('STR#') resource ID }
  24.     kEmptyStringErrorStr    =     1;        { "You must enter a valid value…" }
  25.     
  26.     kEmptyStringError        =    -1;        { error number }
  27.     
  28.     mTextValidated            = 1100;        { actual argument for itsChoice in DoChoice
  29.                                             when text validates sucessfully }
  30.  
  31.  
  32.     
  33.     {###############################################################################
  34.     Global Routines
  35.     ###############################################################################}
  36.     
  37.     PROCEDURE InitUValidText;
  38.     
  39.  
  40. TYPE
  41.     {###############################################################################
  42.     TValidText
  43.         TValidText is an abstract class — that is, it is never going to be
  44.         instantiated directly.  It is used to factor out the common behaviour of
  45.         all subclasses of TEditText for which validation is non-trivial.
  46.     ###############################################################################}
  47.     
  48.     ValidTextTemplatePtr    = ^ValidTextTemplate;
  49.     ValidTextTemplate    = RECORD
  50.         strict:        BOOLEAN;
  51.         required:    BOOLEAN;
  52.         alertID:    INTEGER;
  53.     END;  { ValidTextTemplate }
  54.                                                 
  55.     TValidText = OBJECT(TEditText)
  56.         fStrict:                BOOLEAN;
  57.         fRequired:                BOOLEAN;
  58.         fAlertID:                INTEGER;
  59.         
  60.         PROCEDURE TValidText.IValidText(
  61.                                 itsSuperView:    TView;
  62.                                 itsLocation:    VPoint;
  63.                                 itsSize:        VPoint;
  64.                                 strict:            BOOLEAN;
  65.                                 required:        BOOLEAN;
  66.                                 alertID:        INTEGER);
  67.         
  68.         PROCEDURE TValidText.IRes(
  69.                                 itsDocument:    TDocument;
  70.                                 itsSuperView:    TView;
  71.                             VAR itsParams:        Ptr);
  72.                                 OVERRIDE;
  73.  
  74.         PROCEDURE TValidText.WRes(
  75.                                 theResource:    ViewRsrcHndl;
  76.                             VAR    itsParams:        Ptr);
  77.                                 OVERRIDE;
  78.  
  79.         PROCEDURE TValidText.WriteRes(
  80.                                 theResource:    ViewRsrcHndl;
  81.                             VAR itsParams:        Ptr);
  82.                                 OVERRIDE;
  83.  
  84.         PROCEDURE TValidText.SetStrict(
  85.                                 strict:            BOOLEAN);
  86.             { Set fStrict to the given state.  Note that the current text
  87.                 is always valid, and so it need not be updated to reflect this
  88.                 change. }
  89.  
  90.         PROCEDURE TValidText.SetRequired(
  91.                                 required:        BOOLEAN);
  92.             { Sets fRequired to the given state.  If TRUE, then an empty string
  93.                 will be considered to be invalid. }
  94.         
  95.         FUNCTION  TValidText.GetStrict
  96.                                 :BOOLEAN;
  97.         
  98.         FUNCTION  TValidText.GetRequired
  99.                                 :BOOLEAN;
  100.         
  101.         PROCEDURE TValidText.UpdateText(
  102.                                 redraw:            BOOLEAN);
  103.             { This routine replaces the text in the object with text that
  104.                 reflects the object's current values. }
  105.  
  106.         FUNCTION  TValidText.IsValid(
  107.                             VAR    theText:        Str255;
  108.                             VAR    whyNot:            INTEGER)
  109.                                 :BOOLEAN;
  110.             { If the text is valid, then this function returns TRUE, and whyNot
  111.                 is set to the value noErr (0).  If the text is invalid, then the
  112.                 function returns FALSE and whyNot is set to a value indicating
  113.                 the reason why the text is invalid. }
  114.  
  115.         FUNCTION  TValidText.HandleValidText(
  116.                             VAR    theText:        Str255)
  117.                                 :LONGINT;
  118.             { This routine always returns kValidValue.  In its OVERRIDES, it
  119.                 might update SELF's internal instance variables to reflect
  120.                 the valid text. }
  121.  
  122.         FUNCTION  TValidText.HandleInvalidText(
  123.                             VAR    theText:        Str255;
  124.                                 theError:        INTEGER)
  125.                                 :LONGINT;
  126.             { This routine calls either HandleAlertAccepted() or
  127.               HandleAlertCancelled(), depending on the result returned by a
  128.               call to ValidationErrorAlert(). }
  129.  
  130.         FUNCTION  TValidText.HandleAlertAccepted(
  131.                             VAR    theText:        Str255;
  132.                                 theError:        INTEGER)
  133.                                 :LONGINT;
  134.             { This routine always returns kErrorHandled. Its overrides might
  135.               do something different. }
  136.  
  137.         FUNCTION  TValidText.HandleAlertCancelled(
  138.                             VAR    theText:        Str255;
  139.                                 theError:        INTEGER)
  140.                                 :LONGINT;
  141.             { This routine always returns kErrorHandled. Its overrides might
  142.               do something different. }
  143.  
  144.         PROCEDURE TValidText.ErrorToString(
  145.                                 theError:        INTEGER;
  146.                             VAR    theString:        Str255);
  147.             { This routine sets theString to the string which best explains
  148.                 the given error.  It is intended to be called only from
  149.                 ValidationErrorAlert(), below. }
  150.  
  151.         PROCEDURE TValidText.PrepareErrorAlert(
  152.                             VAR    theText:        Str255;
  153.                                 theError:        INTEGER);
  154.             { The routine sets up the dialog that is displayed by
  155.                 ValidationErrorAlert(), below. }
  156.  
  157.         FUNCTION  TValidText.ValidationErrorAlert(
  158.                             VAR    theText:        Str255;
  159.                                 theError:        INTEGER)
  160.                                 : BOOLEAN;
  161.             { This routine displays the standard MacApp validation error alert.
  162.                 The function result (true by default) indicates whether or not
  163.                 the alert was accepted (true) or cancelled (false). }
  164.         
  165.         FUNCTION  TValidText.Validate
  166.                                 :LONGINT;
  167.                                 OVERRIDE;
  168.             { This routine returns one of the following values:
  169.                 kValidValue:    the text is valid
  170.                 
  171.                 kInvalidValue:    the text is invalid, and a default error alert
  172.                                 should be displayed by MacApp
  173.                                 
  174.                 kErrorHandled:    the text is invalid, but the class handled the
  175.                                 error, so MacApp doesn't have to
  176.             }
  177.                                 
  178.         {$IFC qInspector}
  179.         PROCEDURE TValidText.Fields(
  180.                                 PROCEDURE DoToField(
  181.                                     fieldName:        Str255;
  182.                                     fieldAddr:        Ptr;
  183.                                     fieldType:        INTEGER));
  184.                                 OVERRIDE;
  185.         {$ENDC qInspector}
  186.     END;  { TValidText }
  187.  
  188. IMPLEMENTATION
  189.  
  190.     {$I UValidText.inc1.p}
  191.  
  192. END.  { UValidText.p }